import time
from public.action import *
from public.business import *
from public.testData import *
from public.tools import *
from selenium.webdriver.support.select import Select
import pymysql

# 删除充值记录
connection = pymysql.connect(host="localhost", user="root", password="root", database="dbshop", charset="utf8")
cursor = connection.cursor()
cursor.execute("DELETE FROM dbshop.dbshop_paycheck;")
cursor.execute("commit;")

testCase = "DBshop034_客户管理管理客户_余额调整"
firefox = openBrowser(url)
firefox.get(urlAdmin)
DBshopLoginAdmin(firefox)
DBshopEnterMenu2(firefox, "客户管理", "管理客户")
test_user = "cyy"
add_money = 10000
add_info = "买手机用"
# 调整用户名为xfour的余额
# //td[contains(.,"xfour")]
# 1.点击“余额调整”按钮。
# 获取原有余额
old_balance = float(firefox.find_element_by_xpath('//td[contains(.,"%s")]/following-sibling::td[5]' % test_user).text)
print("调整前余额为%f" % old_balance)
# 轴定位余额调整 //td[contains(.,"xfour")]/following-sibling::td[5]
# 2.添加客户cyy,金额增加"10000",备注内容“买手机用”
change_balance = firefox.find_element_by_xpath('//td[contains(.,"%s")]/following-sibling::td/a[2]' % test_user)
change_balance.click()
firefox.find_element_by_id('money_change_num').send_keys(add_money)
firefox.find_element_by_id('money_pay_info').send_keys(add_info)
firefox.find_element_by_xpath('//button[text()="提交充值"]').click()
time.sleep(2)
firefox.find_element_by_link_text("后台首页").click()
DBshopEnterMenu3(firefox, "客户管理", "客户余额", "充值记录")
# 获取后台完成充值的时间，应该位于第一行//tr[2]/td[1]
completed_time = firefox.find_element_by_xpath("//tr[2]/td[1]").text
print("于%s，充值余额%f成功" % (completed_time, add_money))
# 回到前台
DBshopLogin(firefox, userName, password)
firefox.find_element_by_link_text("用户中心").click()
firefox.find_element_by_link_text("账户余额").click()
new_balance = float(firefox.find_element_by_xpath('//td[contains(.,"%s")]/following-sibling::td[2]'
                                                  % completed_time).text)
print("前台显示在%s充值后余额为%f" % (completed_time, new_balance))
# 3.点击“提交充值”按钮。在前台查询cyy是否余额多10000.

if new_balance - old_balance == add_money:
    if firefox.find_element_by_xpath('//td[contains(.,"%s")]/following-sibling::td[2]'
                                     % completed_time).text == add_info:
        writeLog("%s 测试通过" % testCase)
else:
    writeLog("%s 测试失败" % testCase)
    getPhoto(firefox)
firefox.quit()  # quit()函数有2个作用 1.关闭所有页面 2.关闭浏览器对象 quit()函数建议放到脚本的最后